package com.example.design.shejimoshi.action.expression.improve02;

import java.util.Map;

/**
 * @Description TODO
 * @date 2022/11/11 17:06
 * @Author liuzhihui
 * @Version 1.0
 */
public class GreaterExpression implements Expression {
    private String key;
    private long value;

    public GreaterExpression(String strExpression) {
        String[] elements = strExpression.trim().split("\\s+");
        if (elements.length != 3 || !elements[1].trim().equals(">")) {
            throw new RuntimeException("Expression is invalid: " + strExpression);
        }
        this.key = elements[0].trim();
        this.value = Long.parseLong(elements[2].trim());
    }

    public GreaterExpression(String key, long value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public boolean interpret(Map<String, Long> stats) {
        if (!stats.containsKey(key)) {
            return false;
        }

        Long statValue = stats.get(key);

        return statValue > value;
    }

    public static void main(String[] args) {
        String rule = "key1 > 100 && key2 < 30 || key3 < 100 || key4 == 88";
        // \表示java中的转译,\s表示空格,+表示多个
        String[] elements = rule.trim().split("\\s+");
        System.out.println(elements);
    }
}
